<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-row>
						<a-col :span="12">
							<a-form-item label="所属渠道" v-bind="validateInfos.channelCode">
								<j-dict-select-tag v-model:value="formData.channelCode" dictCode="base_sale_channel,channel_name,channel_code" placeholder="请选择所属渠道" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位名称" v-bind="validateInfos.companyName">
								<a-input v-model:value="formData.companyName" placeholder="请输入单位名称" ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位简称" v-bind="validateInfos.shortName">
								<a-input v-model:value="formData.shortName" placeholder="请输入单位简称" ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位类型" v-bind="validateInfos.companyType">
								<j-dict-select-tag v-model:value="formData.companyType" dictCode="dict_company_type" placeholder="请选择单位类型" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="行业类型" v-bind="validateInfos.industryType">
								<j-dict-select-tag v-model:value="formData.industryType" dictCode="dict_industry_type" placeholder="请选择行业类型" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="社会信用代码" v-bind="validateInfos.creditCode">
								<a-input v-model:value="formData.creditCode" placeholder="请输入社会信用代码" ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="地区" v-bind="validateInfos.district">
								<j-area-linkage v-model:value="formData.district" placeholder="请输入地区" saveCode="region"  />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="注册资本" v-bind="validateInfos.registerCapital">
								<a-input-number v-model:value="formData.registerCapital" placeholder="请输入注册资本" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="成立时间" v-bind="validateInfos.establishDate">
								<a-date-picker placeholder="请选择成立时间" v-model:value="formData.establishDate" value-format="YYYY-MM-DD"  style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="开始日期" v-bind="validateInfos.startTerm">
								<a-date-picker placeholder="请选择开始日期" v-model:value="formData.startTerm" value-format="YYYY-MM-DD"  style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="结束日期" v-bind="validateInfos.endTerm">
								<a-date-picker placeholder="请选择结束日期" v-model:value="formData.endTerm" value-format="YYYY-MM-DD"  style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="登记机关" v-bind="validateInfos.registrationAuthority">
								<a-input v-model:value="formData.registrationAuthority" placeholder="请输入登记机关" ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="经营地址" v-bind="validateInfos.address">
								<a-input v-model:value="formData.address" placeholder="请输入经营地址" ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="经营范围" v-bind="validateInfos.businessScope">
								<a-textarea v-model:value="formData.businessScope" :rows="4" placeholder="请输入经营范围" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="主营业务" v-bind="validateInfos.businessExpertise">
								<a-textarea v-model:value="formData.businessExpertise" :rows="4" placeholder="请输入主营业务" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位介绍" v-bind="validateInfos.introduction">
								<a-textarea v-model:value="formData.introduction" :rows="4" placeholder="请输入单位介绍" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="营业执照" v-bind="validateInfos.businessLicense">
								<j-image-upload  v-model:value="formData.businessLicense" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位logo" v-bind="validateInfos.logo">
								<j-image-upload  v-model:value="formData.logo" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="认证状态" v-bind="validateInfos.status">
								<j-dict-select-tag type='radio' v-model:value="formData.status" dictCode="dict_company_status" placeholder="请选择认证状态" />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JAreaLinkage from '/@/components/Form/src/jeecg/components/JAreaLinkage.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../BaseCompany.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    channelCode: '',   
    companyName: '',   
    shortName: '',   
    companyType: '',   
    industryType: '',   
    creditCode: '',   
    district: '',   
    registerCapital: undefined,
    establishDate: '',   
    startTerm: '',   
    endTerm: '',   
    registrationAuthority: '',   
    address: '',   
    businessScope: '',   
    businessExpertise: '',   
    introduction: '',   
    businessLicense: '',   
    logo: '',   
    status: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    channelCode: [{ required: true, message: '请输入所属渠道!'},],
    companyName: [{ required: true, message: '请输入单位名称!'},],
    shortName: [{ required: true, message: '请输入单位简称!'},],
    companyType: [{ required: true, message: '请输入单位类型!'},],
    industryType: [{ required: true, message: '请输入行业类型!'},],
    creditCode: [{ required: true, message: '请输入社会信用代码!'},],
    district: [{ required: true, message: '请输入地区!'},],
    registerCapital: [{ required: true, message: '请输入注册资本!'},],
    establishDate: [{ required: true, message: '请输入成立时间!'},],
    startTerm: [{ required: true, message: '请输入开始日期!'},],
    endTerm: [{ required: true, message: '请输入结束日期!'},],
    registrationAuthority: [{ required: true, message: '请输入登记机关!'},],
    address: [{ required: true, message: '请输入经营地址!'},],
    businessScope: [{ required: true, message: '请输入经营范围!'},],
    businessExpertise: [{ required: true, message: '请输入主营业务!'},],
    introduction: [{ required: true, message: '请输入单位介绍!'},],
    businessLicense: [{ required: true, message: '请输入营业执照!'},],
    logo: [{ required: true, message: '请输入单位logo!'},],
    status: [{ required: true, message: '请输入认证状态!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
